There is a built ing function in Python which is used for opening files. The function is called open() and takes two arguments:
Opening mode tells Python for what reason to open the file. The possible values for that argument are:
The open() function works closely with the close() function, which tells Python to close the opened file.
Let's, for example, write the lyrics of John Lennong's "Imagine" song to a new text file. Then that file will be used for reading purposes.
In [1]:
lyrics = "Imagine all the people, \nliving life in peace... \n\tJohn Lennon"
print(lyrics)
In [2]:
our_file = open("imagine_lyrics.txt","w")
So now a new text file titled "imagine_lyrics" was created for writing purposes (please take a look to the file dimension .txt which was mandatory to include). We can now use the write() function to write different text to our new file. We will write there the lyrics.
In [3]:
our_file.write("Imagine by John Lennon")
In [4]:
our_file.write(lyrics)
As mentioned above, once the actions with the opened file are complete, you may already close it.
In [5]:
our_file.close()
Once the file already exists, we can now read it back to Python. Again, we must open it, but in read only mode this time.
In [6]:
lyrics_imported = open("imagine_lyrics.txt","r")
In [7]:
print(lyrics_imported)
As you can see above, hte file was succesfully read, but printing it gives only some info, and not the content of the file. For that reason, the read only opened file should also be read, using the built-in read() function.
In [8]:
lyrics_imported.read()
Out[8]:
In [10]:
print lyrics_imported.read()
If you give any integer as an argument to read() function, then it will return only that much very first characters of the string (e.g. read(7) would return "Imagine", while read(4) would return "Imag").
One thing you may have already noticed about read() function is that it returns one full string from the text file. Yet, you might also be interested in reading the file line-by-line. For that purpose, the readlines() function can come handy.
In [19]:
lyrics_imported.readlines()
Out[19]:
In Jupyter notebook (this is specific to Jupyter notebook only), the opened read only file can be read only once. So In order to read it again, we have to open it once again.
In [33]:
lyrics_imported = open("imagine_lyrics.txt","r")
In [34]:
lyrics_imported.readlines()
Out[34]:
As you can see, not only it read each line separately, but the output we received has the type of list. We may check it, if we save the results in a new file.
In [35]:
lyrics_imported = open("imagine_lyrics.txt","r")
lyrics_list = lyrics_imported.readlines()
In [38]:
type(lyrics_list)
Out[38]:
Fine, now we are sure that the output is a list, so we can perform different actions on it
In [39]:
lyrics_list[0]
Out[39]:
In [40]:
lyrics_list[1]
Out[40]:
Once verything is done, we should remember to close the file down.
In [42]:
lyrics_imported.close()
Final note: besides readlines() function there is only the singular version called readline() which returns a single line from the text file (e.h. readline(3) will return the whole content of the 3rd line only).
One of the main drawbacks of the above approach is that you shouls always remember to close the files once your actions are completed. A good shortcut is to write all content to a new variable, and close your file immediately. For that purpose a very handy statement called with exists, which closes the file itself. Let's try it out.
In [43]:
with open("imagine_lyrics.txt","r") as data_imported:
data = data_imported.readlines()
As it can be seen above, no close function was used, as the with statement automatically closes the file down. Another important thing to note, is that the data_imported varible was a temporary name used for an imported file to perform actions on. So we wrote its content to a new global variable (called data) that can be used further for analysis.
In [44]:
print data
In [45]:
print data_imported